#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import json
from datetime import datetime

for name in ['lxy','jzy','xk','jxs','kzz','qd']:
    result_file_name = f'real/对比_{name}.csv'
    print(result_file_name)
    with open(result_file_name, 'w', newline='', encoding='utf-8') as csvfile,open(f'real/result_{name}.json','r',encoding='utf-8') as json_f:
        # 获得 writer对象 delimiter是分隔符 默认为 ","
        writer = csv.writer(csvfile, delimiter=',', dialect='excel')
        # 调用 writer的 writerow方法将 test_writer_data写入 test_writer.csv文件
        heade_row = ["企业名称","成立年份","探迹数量","友商数量","不一致号码1","来源名称","来源URL","不一致号码2","来源名称","来源URL","不一致号码3","来源名称","来源URL"]
        writer.writerow(heade_row)
        result = json.load(json_f)
        for ent_name,dic in result.items():
            start_year = str(dic.get('start_year','')).strip()[:4]
            tj_phone_count = dic.get('tj_phone_count')
            ys_phone_count = dic.get('ys_phone_count')
            if not ys_phone_count:
                continue
            one_row = [ent_name,start_year,tj_phone_count,ys_phone_count]
            phone_ls = dic.get('phone_ls',[])
            # if not phone_ls:
            #     continue
            for phone_dic in phone_ls:
                phone = phone_dic.get('phone')
                _source_ls = phone_dic.get('source_ls',[])
                source_ls = [i for i in _source_ls if i]
                _source_href_ls = phone_dic.get('source_href_ls',[])
                source_href_ls = [i for i in _source_href_ls if i]
                one_row.extend([phone,source_ls,source_href_ls])

            writer.writerow(one_row)




























